//
//  AppDelegate+AppConfiguration.m
//  JSBaseProject
//
//  Created by Apple on 2017/11/7.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "AppDelegate+AppConfiguration.h"
#import "QMUIConfigurationTemplate.h"
#import <Bugly/Bugly.h>
#import "JSBaseNaviController.h"
#import "JSBaseViewController.h"
#import "JSBaseTabBarController.h"

@implementation AppDelegate (AppConfiguration)

-(void) setupAppConfiguration{
    // 应用 QMUI Demo 皮肤
    NSString *themeClassName = [[NSUserDefaults standardUserDefaults] stringForKey:QDSelectedThemeClassName] ?: NSStringFromClass([QMUIConfigurationTemplate class]);
    [QDThemeManager sharedInstance].currentTheme = [[NSClassFromString(themeClassName) alloc] init];
    
    // QD自定义的全局样式渲染
    [QDCommonUI renderGlobalAppearances];
    
    // 预加载 QQ 表情，避免第一次使用时卡顿（可选）
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [QMUIQQEmotionManager emotionsForQQ];
    });
    
    [[UIButton appearance] setExclusiveTouch:YES];
    
    if (@available(iOS 11.0, *)) {
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    }
}
//icon_tabbar_1_selected
-(void) setupAppViewControllers{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // 获取根视图信息
    NSDictionary *rootVCs = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"rootViewControllerList" ofType:@"plist"]];
    NSArray *vcArray = rootVCs[@"viewControllrs"];
    NSInteger i = 0;
    NSMutableArray *naviArray = @[].mutableCopy;
    for (NSDictionary *subVCInfo in vcArray) {
        NSString *title = subVCInfo[@"title"];
        NSString *image = subVCInfo[@"icon_tabBar"];
        NSString *slectImage = subVCInfo[@"icon_tabBar_select"];
        NSString *className  = subVCInfo[@"className"];
        UIViewController *vc = [[NSClassFromString(className) alloc] init];
        vc.hidesBottomBarWhenPushed = NO;
        JSBaseNaviController *navi = [[JSBaseNaviController alloc] initWithRootViewController:vc];
        navi.tabBarItem = [QDUIHelper tabBarItemWithTitle:title image:[UIImageMake(image) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:UIImageMake(slectImage) tag:i];
        [naviArray addObject:navi];
        i++;
    }
    JSBaseTabBarController *tabBarViewController = [[JSBaseTabBarController alloc] init];
    // window root controller
    tabBarViewController.viewControllers = naviArray.copy;
    self.window.rootViewController = tabBarViewController;
    [self.window makeKeyAndVisible];
}

-(void) setupAppFitstUsering{
    QMUILog(@"首次进入配置");
}
-(void) setupAppThirdConfigurarion{
    QMUILog(@"第三方配置");
    [Bugly startWithAppId:@"e4b144bff9"];

    
}
- (void)startLaunchingAnimation {
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    UIView *launchScreenView = [[NSBundle mainBundle] loadNibNamed:@"LaunchScreen" owner:self options:nil].firstObject;
    launchScreenView.frame = window.bounds;
    [window addSubview:launchScreenView];
    
    UIImageView *backgroundImageView = launchScreenView.subviews[0];
    backgroundImageView.clipsToBounds = YES;
    
    UIImageView *logoImageView = launchScreenView.subviews[1];
    UILabel *copyrightLabel = launchScreenView.subviews.lastObject;
    
    UIView *maskView = [[UIView alloc] initWithFrame:launchScreenView.bounds];
    maskView.backgroundColor = UIColorWhite;
    [launchScreenView insertSubview:maskView belowSubview:backgroundImageView];
    
    [launchScreenView layoutIfNeeded];
    
    
    [launchScreenView.constraints enumerateObjectsUsingBlock:^(__kindof NSLayoutConstraint * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.identifier isEqualToString:@"bottomAlign"]) {
            obj.active = NO;
            [NSLayoutConstraint constraintWithItem:backgroundImageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:launchScreenView attribute:NSLayoutAttributeTop multiplier:1 constant:NavigationContentTop].active = YES;
            *stop = YES;
        }
    }];
    
    [UIView animateWithDuration:.15 delay:0.9 options:QMUIViewAnimationOptionsCurveOut animations:^{
        [launchScreenView layoutIfNeeded];
        logoImageView.alpha = 0.0;
        copyrightLabel.alpha = 0;
    } completion:nil];
    [UIView animateWithDuration:1.2 delay:0.9 options:UIViewAnimationOptionCurveEaseOut animations:^{
        maskView.alpha = 0;
        backgroundImageView.alpha = 0;
    } completion:^(BOOL finished) {
        [launchScreenView removeFromSuperview];
    }];
}
@end
